package com.groupbuying.redpacket.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.groupbuying.redpacket.constants.MQConstans;
import com.groupbuying.redpacket.dao.RedPacketListDao;
import com.groupbuying.redpacket.dto.RedPacketInfoTransferDTO;
import com.groupbuying.redpacket.service.RedPacketInfoTransferService;
import com.groupbuying.redpacket.utils.AesUtils;
import com.groupbuying.redpacket.utils.BeanOrMapConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName RedPacketInfoTransferServiceImpl
 * @Description: TODO   红包信息传输业务层实现类
 * @Author srlg3
 * @Date 2019/11/26
 * @Version V1.0
 **/
@Service
public class RedPacketInfoTransferServiceImpl implements RedPacketInfoTransferService {

    Logger logger = LoggerFactory.getLogger(RedPacketInfoTransferServiceImpl.class);

    @Autowired
    private RedPacketListDao redPacketListDao;

    @Autowired
    private Producer producer;

    @Override
    public boolean redPacketInfoTransferToMq(RedPacketInfoTransferDTO redPacketInfoTransferDTO) {

        //1. 判断
        if (redPacketInfoTransferDTO == null) {
            logger.error("错误提示：传输红包信息不能为空");
            return false;
        }

        logger.info("传输信息:{}",redPacketInfoTransferDTO.toString());

        //2. 加密
        logger.info("提示：开始发送mq");

        //加密前参数
        String enBefore;

        try {
            enBefore = JSON.toJSONString(BeanOrMapConvertUtils.objectToMap(redPacketInfoTransferDTO));
        } catch (Exception e) {
            logger.error("传输转换object-map有误");
            return false;
        }

        //加密后参数
        String enAfter= AesUtils.encryptAES(enBefore,AesUtils.password);

        //4. 发送mq
        try {
            Message message = new Message(MQConstans.REDPACKET_ADDAMT_TOPIC,
                    MQConstans.REDPACKET_ADDAMT_TAG,enAfter.getBytes());
            producer.send(message);
        } catch (Exception e) {
            logger.info("提示：发送添加红包金额MQ出现错误");
            return false;
        }
        logger.info("提示：发送mq成功");

        return true;
    }
}
